package com.seckill.controller;

import com.seckill.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String username,
                                     @RequestParam String password,
                                     @RequestParam(required = false) String ip,
                                     @RequestParam(required = false) String deviceId) {
        String token = userService.login(username, password, ip, deviceId);
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        return result;
    }

    /**
     * 检查用户是否符合秒杀资格（如VIP等级、黑名单）
     * @param username 用户名
     * @param ipAddress IP地址
     * @param deviceId 设备ID
     * @return qualified: 是否有资格，msg: 提示信息
     */
    @GetMapping("/check")
    public Map<String, Object> checkSeckillQualification(@RequestParam String username,
                                                        @RequestParam(required = false) String ipAddress,
                                                        @RequestParam(required = false) String deviceId) {
        Map<String, Object> result = new HashMap<>();
        boolean qualified = userService.checkSeckillQualification(username, ipAddress, deviceId);
        result.put("qualified", qualified);
        result.put("msg", qualified ? "用户符合秒杀资格" : "不符合秒杀资格（可能因VIP等级或黑名单）");
        return result;
    }
} 